#include "img_info.h"

static int total_num = 0;

int IsDir(const char*filename)
{
	struct stat st;
	if (stat(filename, &st) == -1) return 0;
	return S_ISDIR(st.st_mode);
}

int res_img_search(const char * dir)
{
	DIR *   dirfd = NULL;
	struct  dirent  *entry;
    char	filename[256]={0};
    char	buff_T[sizeof(struct dirent) + 1024];
	
	dirfd = opendir(dir);
	if(!dirfd){
		debug_e("open dir error\n");
		return -1;
	}

	entry = (struct dirent *)buff_T;
	while (1)
	{
		entry = readdir(dirfd);
		if (!entry)
			break;

        memset(filename,'\0',sizeof(filename));
        strcpy(filename,dir);
        strcat(filename,"/");
        strcat(filename,entry->d_name);

        if(!strcmp(entry->d_name, "."))
        {
            continue;
        }
        if(!strcmp(entry->d_name, ".."))
        {
            continue;
        }
        if(!strcmp(entry->d_name, "Heat"))
        {
            continue;
        }
        if(!strcmp(entry->d_name, "infoball"))
        {
            continue;
        }
        if(!strcmp(entry->d_name, "KeepWarm"))
        {
            continue;
        }
        if(!strcmp(entry->d_name, "Loading"))
        {
            continue;
        }
        if(!strcmp(entry->d_name, "MideaLogo"))
        {
            continue;
        }
        if(strstr(entry->d_name, "BG"))
        {
            continue;
        }       
                
        if(IsDir(filename))
        {
            res_img_search(filename);
        }
        else
        {
            if(!strstr(entry->d_name, ".png"))
            {
                continue;
            }

            //debug_d("file=%s\n",entry->d_name);
            total_num++;

            img_queue_add(filename);
        }
	}

	closedir(dirfd);
	dirfd = NULL;

	return 0;
}

int res_img_scan_all(const char * dir)
{
    res_img_search(dir);

    debug_d("num=%d\n",total_num);

    img_queue_decode_all();

    debug_d("decode finish\n");

    return 0;
}
